package com.gitee.ywj1352.leecode;

import org.junit.Test;

/**
 * @author wenjie.yang on 2019/10/10.
 */
public class 尾递归 {


    /**
     * 尾递归
     *
     * @param n
     * @param r1
     * @param r2
     * @return
     */
    public int fbnq2(int n, int r1, int r2) {
        if (n == 0) {
            return r1;
        }
        return fbnq2(n - 1, r2, r1 + r2);
    }

    @Test
    public void fun1() {

        int i = fbnq2(43, 0, 1);
        System.out.println(i);

    }

    /**
     * 使用尾递归 计算
     * @param n
     * @param r
     * @return
     */
    public int jc(int n, int r) {
        if (n <= 1) {
            return r;
        }
        return jc(n - 1, r *n);
    }

    @Test
    public void fun2() {
        int i = jc(7, 1);
        System.out.println(i);
    }


}
